<template>
    <div class="assess_form nerve_injury">
        <div class="form_title">XXXX医疗机构</div>
        <div class="form_name">神经损伤功能评定表</div>
        <div class="form_patient_bar">
            <div v-for="(item, index) in patientStruct" :key="index" class="patient_info_item">
                <div class="item_label">{{ item.label }}：</div>
                <div class="item_value">{{ formType == 'template' ? item.label : patientInfo[item.key] }}{{ item.unit }}</div>
            </div>
        </div>
        <div class="form_diagnosis">
            <div class="diagnosis_label">诊断：</div>
            <div class="diagnosis_value">{{ formType == 'template' ? '诊断' : selfFormData.diagnosticInfo }}</div>
        </div>
        <el-form ref="assessForm" :model="selfFormData" :rules="rules">
            <el-form-item class="assess_term" prop="estimateType">
                <el-radio-group v-if="formType !== 'view'" v-model="selfFormData.estimateType" :disabled="formType == 'template'">
                    <el-radio :label="1" :disabled="!!selfFormData.evaluateUser1">
                        <div class="term_item">
                            <div class="term_name">初期评定</div>
                            <div class="term_time">评定时间：{{ selfFormData.estimateType === 1 ? selfFormData.evaluateTime :
                                (selfFormData.evaluateTime1 ? selfFormData.evaluateTime1 : '') }}</div>
                            <div class="term_sign">签名：{{ selfFormData.estimateType === 1 ? selfFormData.evaluateUser :
                                (selfFormData.evaluateUser1 ? selfFormData.evaluateUser1 : '') }}</div>
                        </div>
                    </el-radio>
                    <el-radio :label="2" :disabled="!!selfFormData.evaluateUser2">
                        <div class="term_item">
                            <div class="term_name">中期评定</div>
                            <div class="term_time">评定时间：{{ selfFormData.estimateType === 2 ? selfFormData.evaluateTime :
                                (selfFormData.evaluateTime2 ? selfFormData.evaluateTime2 : '') }}</div>
                            <div class="term_sign">签名：{{ selfFormData.estimateType === 2 ? selfFormData.evaluateUser :
                                (selfFormData.evaluateUser2 ? selfFormData.evaluateUser2 : '') }}</div>
                        </div>
                    </el-radio>
                    <el-radio :label="3" :disabled="!!selfFormData.evaluateUser3">
                        <div class="term_item">
                            <div class="term_name">出院前评定</div>
                            <div class="term_time">评定时间：{{ selfFormData.estimateType === 3 ? selfFormData.evaluateTime :
                                (selfFormData.evaluateTime3 ? selfFormData.evaluateTime3 : '') }}</div>
                            <div class="term_sign">签名：{{ selfFormData.estimateType === 3 ? selfFormData.evaluateUser :
                                (selfFormData.evaluateUser3 ? selfFormData.evaluateUser3 : '') }}</div>
                        </div>
                    </el-radio>
                </el-radio-group>
                <div v-else class="assess_term">
                    <div class="term_item">
                        <div class="term_name">初期评定
                            <el-checkbox style="margin-left: 5px;" disabled />
                        </div>
                        <div class="term_time">评定时间：{{ selfFormData.evaluateTime1 }}</div>
                        <div class="term_sign">签名：{{ selfFormData.evaluateUser1 }}</div>
                    </div>
                    <div class="term_item">
                        <div class="term_name">中期评定
                            <el-checkbox style="margin-left: 5px;" disabled />
                        </div>
                        <div class="term_time">评定时间：{{ selfFormData.evaluateTime2 }}</div>
                        <div class="term_sign">签名：{{ selfFormData.evaluateUser2 }}</div>
                    </div>
                    <div class="term_item">
                        <div class="term_name">出院前评定
                            <el-checkbox style="margin-left: 5px;" disabled />
                        </div>
                        <div class="term_time">评定时间：{{ selfFormData.evaluateTime3 }}</div>
                        <div class="term_sign">签名：{{ selfFormData.evaluateUser3 }}</div>
                    </div>
                </div>
            </el-form-item>
            <div>1.肌张力检测（改良Ashworth）：</div>
            <el-form-item v-if="formType === 'edit' || formType === 'template'" label="检测侧：" prop="tensionType">
                <el-radio-group v-model="selfFormData.tensionType" :disabled="!firstAssess || formType === 'template'">
                    <el-radio v-for="(option, index) in typeList" :key="'option1_' + index" :label="option.value">{{
                        option.label }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="ashworth">
                <table border class="form_table">
                    <colgroup>
                        <col style="width: 140px;">
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col style="width: 140px;">
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                    </colgroup>
                    <thead>
                        <tr>
                            <th class="text_center">{{ getLabelByValue(typeList, selfFormData.tensionType) }}</th>
                            <th v-if="formType !== 'view'" class="text_center">评估</th>
                            <th v-if="formType === 'view'" class="text_center">初评</th>
                            <th v-if="formType === 'view'" class="text_center">中评</th>
                            <th v-if="formType === 'view'" class="text_center">末评</th>
                            <th class="text_center">{{ getLabelByValue(typeList, selfFormData.tensionType) }}</th>
                            <th v-if="formType !== 'view'" class="text_center">评估</th>
                            <th v-if="formType === 'view'" class="text_center">初评</th>
                            <th v-if="formType === 'view'" class="text_center">中评</th>
                            <th v-if="formType === 'view'" class="text_center">末评</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="index in 4" :key="'assess1_' + index">
                            <td class="text_center">{{ assessFormStruct1[index - 1].content }}</td>
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-radio-group v-model="selfFormData.ashworth[assessFormStruct1[index - 1].editKey]"
                                    class="score_radio" :disabled="formType === 'template'">
                                    <el-radio v-for="(option, optionIndex) in tensionValueList"
                                        :key="'tension' + (index - 1) + '_' + optionIndex" :label="option.value">{{
                                        option.label }}</el-radio>
                                </el-radio-group>
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index - 1].firstKey]) }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index - 1].midKey]) }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index - 1].lastKey]) }}</td>
                            <td class="text_center">{{ assessFormStruct1[index + 3].content }}</td>
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-radio-group v-model="selfFormData.ashworth[assessFormStruct1[index + 3].editKey]"
                                    class="score_radio" :disabled="formType === 'template'">
                                    <el-radio v-for="(option, optionIndex) in tensionValueList"
                                        :key="'tension' + (index + 3) + '_' + optionIndex" :label="option.value">{{
                                        option.label }}</el-radio>
                                </el-radio-group>
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index + 3].firstKey]) }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index + 3].midKey]) }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ getLabelByValue(tensionValueList,
                                selfFormData.ashworth[assessFormStruct1[index + 3].lastKey]) }}</td>
                        </tr>
                    </tbody>
                </table>
            </el-form-item>
            <div>2.徒手肌力检测（MMT）：</div>
            <el-form-item v-if="formType === 'edit' || formType === 'template'" label="检测侧：" prop="unarmedType">
                <el-radio-group v-model="selfFormData.unarmedType" :disabled="!firstAssess || formType === 'template'">
                    <el-radio v-for="(option, index) in typeList" :key="'option2_' + index" :label="option.value">{{
                        option.label }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="mmt">
                <table border class="form_table">
                    <colgroup>
                        <col style="width: 50px;">
                        <col style="width: 90px;">
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col style="width: 50px;">
                        <col style="width: 90px;">
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                    </colgroup>
                    <thead>
                        <tr>
                            <th class="text_center">{{ getLabelByValue(typeList, selfFormData.unarmedType) }}</th>
                            <th class="text_center">动作</th>
                            <th v-if="formType !== 'view'" class="text_center">评估</th>
                            <th v-if="formType === 'view'" class="text_center">初评</th>
                            <th v-if="formType === 'view'" class="text_center">中评</th>
                            <th v-if="formType === 'view'" class="text_center">末评</th>
                            <th class="text_center">{{ getLabelByValue(typeList, selfFormData.unarmedType) }}</th>
                            <th class="text_center">动作</th>
                            <th v-if="formType !== 'view'" class="text_center">评估</th>
                            <th v-if="formType === 'view'" class="text_center">初评</th>
                            <th v-if="formType === 'view'" class="text_center">中评</th>
                            <th v-if="formType === 'view'" class="text_center">末评</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="index in 12" :key="'assess2_' + index">
                            <td v-if="!assessFormStruct2[index - 1] || assessFormStruct2[index - 1].rowspan > 0"
                                class="text_center"
                                :rowspan="assessFormStruct2[index - 1] ? assessFormStruct2[index - 1].rowspan : 1">{{
                                    assessFormStruct2[index - 1] ? assessFormStruct2[index - 1].part : '' }}</td>
                            <td class="text_center">{{ assessFormStruct2[index - 1] ? assessFormStruct2[index -
                                1].content : '' }}</td>
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-radio-group v-if="assessFormStruct2[index - 1]"
                                    v-model="selfFormData.mmt[assessFormStruct2[index - 1].editKey]"
                                    class="score_radio" :disabled="formType === 'template'">
                                    <el-radio v-for="(option, optionIndex) in unarmedValueList"
                                        :key="'unarmed' + (index - 1) + '_' + optionIndex" :label="option.value">{{
                                        option.label }}</el-radio>
                                </el-radio-group>
                                <span v-else />
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index - 1] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index -
                                1].firstKey]) : '' }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index - 1] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index - 1].midKey])
                                : '' }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index - 1] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index -
                                1].lastKey]) : '' }}</td>
                            <td v-if="!assessFormStruct2[index + 11] || assessFormStruct2[index + 11].rowspan > 0"
                                class="text_center"
                                :rowspan="assessFormStruct2[index + 11] ? assessFormStruct2[index + 11].rowspan : 1">{{
                                    assessFormStruct2[index + 11] ? assessFormStruct2[index + 11].part : '' }}</td>
                            <td class="text_center">{{ assessFormStruct2[index + 11] ? assessFormStruct2[index +
                                11].content : '' }}</td>
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-radio-group v-if="assessFormStruct2[index + 11]"
                                    v-model="selfFormData.mmt[assessFormStruct2[index + 11].editKey]"
                                    class="score_radio" :disabled="formType === 'template'">
                                    <el-radio v-for="(option, optionIndex) in unarmedValueList"
                                        :key="'unarmed' + (index + 11) + '_' + optionIndex" :label="option.value">{{
                                        option.label }}</el-radio>
                                </el-radio-group>
                                <span v-else />
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index + 11] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index +
                                11].firstKey]) : '' }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index + 11] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index +
                                11].midKey]) : '' }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ assessFormStruct2[index + 11] ?
                                getLabelByValue(unarmedValueList, selfFormData.mmt[assessFormStruct2[index +
                                11].lastKey]) : '' }}</td>
                        </tr>
                    </tbody>
                </table>
            </el-form-item>
            <div>3.关节活动度：</div>
            <el-form-item prop="rom">
                <table border class="form_table">
                    <colgroup>
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col style="width: 70px;">
                        <col style="width: 115px;">
                        <col style="width: 120px;">
                        <col v-if="formType !== 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                        <col v-if="formType === 'view'">
                    </colgroup>
                    <thead>
                        <tr>
                            <th class="text_center" :colspan="formType === 'view' ? 3 : 1">左侧</th>
                            <th class="text_center" :rowspan="formType === 'view' ? 2 : 1">部位</th>
                            <th class="text_center" :rowspan="formType === 'view' ? 2 : 1">动作</th>
                            <th class="text_center" :rowspan="formType === 'view' ? 2 : 1">活动度</th>
                            <th class="text_center" :colspan="formType === 'view' ? 3 : 1">右侧</th>
                        </tr>
                        <tr v-if="formType === 'view'">
                            <th class="text_center">初评</th>
                            <th class="text_center">中评</th>
                            <th class="text_center">末评</th>
                            <th class="text_center">初评</th>
                            <th class="text_center">中评</th>
                            <th class="text_center">末评</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="(item, index) in assessFormStruct3" :key="'assess3_' + index">
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-input :class="{valid_input: isValidateItem(index, 'left')}" v-model="selfFormData.rom[item.leftEditKey]" :placeholder="item.levelValue" :disabled="formType === 'template'"/>
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.leftFirstKey] }}
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.leftMidKey] }}
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.leftLastKey] }}
                            </td>
                            <td v-if="item.rowspan > 0" class="text_center" :rowspan="item.rowspan">{{ item.part }}</td>
                            <td class="text_center">{{ item.content }}</td>
                            <td class="text_center">{{ item.levelValue }}</td>
                            <td v-if="formType === 'edit' || formType === 'template'">
                                <el-input :class="{valid_input: isValidateItem(index, 'right')}" v-model="selfFormData.rom[item.rightEditKey]" :placeholder="item.levelValue" :disabled="formType === 'template'"/>
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.rightFirstKey]
                                }}</td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.rightMidKey] }}
                            </td>
                            <td v-if="formType === 'view'" class="text_center">{{ selfFormData.rom[item.rightLastKey] }}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import moment from 'moment'
const initDefaultForm = () => {
    const ashworth = {}
    for (let ashworthIndex = 1; ashworthIndex <= 8; ashworthIndex++) {
        ashworth['item' + ashworthIndex] = undefined
    }

    const mmt = {}
    for (let mmtIndex = 1; mmtIndex <= 20; mmtIndex++) {
        mmt['item' + mmtIndex] = undefined
    }

    const rom = {}
    for (let romIndex = 1; romIndex <= 12; romIndex++) {
        rom['leftItem' + romIndex] = ''
        rom['rightItem' + romIndex] = ''
    }
    return {
        diagnosticInfo: '',
        estimateType: undefined,
        tensionType: undefined,
        unarmedType: undefined,
        ashworth,
        mmt,
        rom,
        evaluateUser: '',
        evaluateTime: ''
    }
}
const typeList = [
    {
        label: '左侧',
        value: 1
    },
    {
        label: '右侧',
        value: 2
    },
    {
        label: '双侧',
        value: 3
    }
]

const tensionValueList = [
    {
        label: '0级',
        value: 0
    },
    {
        label: '1级',
        value: 1
    },
    {
        label: '1+级',
        value: 2
    },
    {
        label: '2级',
        value: 3
    },
    {
        label: '3级',
        value: 4
    },
    {
        label: '4级',
        value: 5
    }
]
const unarmedValueList = [
    {
        label: '0级',
        value: 0
    },
    {
        label: '1级',
        value: 1
    },
    {
        label: '2级',
        value: 2
    },
    {
        label: '3级',
        value: 3
    },
    {
        label: '4级',
        value: 4
    },
    {
        label: '5级',
        value: 5
    }
]
const assessFormStruct1 = [
    {
        content: '肩：屈/伸',
        editKey: 'item1',
        firstKey: 'firstAshworthItem1',
        midKey: 'midAshworthItem1',
        lastKey: 'lastAshworthItem1'
    },
    {
        content: '肘：屈/伸',
        editKey: 'item2',
        firstKey: 'firstAshworthItem2',
        midKey: 'midAshworthItem2',
        lastKey: 'lastAshworthItem2'
    },
    {
        content: '腕：屈/伸',
        editKey: 'item3',
        firstKey: 'firstAshworthItem3',
        midKey: 'midAshworthItem3',
        lastKey: 'lastAshworthItem3'
    },
    {
        content: '指：屈/伸',
        editKey: 'item4',
        firstKey: 'firstAshworthItem4',
        midKey: 'midAshworthItem4',
        lastKey: 'lastAshworthItem4'
    },
    {
        content: '髋：前屈/后伸',
        editKey: 'item5',
        firstKey: 'firstAshworthItem5',
        midKey: 'midAshworthItem5',
        lastKey: 'lastAshworthItem5'
    },
    {
        content: '髋：外展/内收',
        editKey: 'item6',
        firstKey: 'firstAshworthItem6',
        midKey: 'midAshworthItem6',
        lastKey: 'lastAshworthItem6'
    },
    {
        content: '膝：屈/伸',
        editKey: 'item7',
        firstKey: 'firstAshworthItem7',
        midKey: 'midAshworthItem7',
        lastKey: 'lastAshworthItem7'
    },
    {
        content: '踝：背屈/跖屈',
        editKey: 'item8',
        firstKey: 'firstAshworthItem8',
        midKey: 'midAshworthItem8',
        lastKey: 'lastAshworthItem8'
    }
]
const assessFormStruct2 = [
    {
        part: '肩',
        content: '前屈',
        editKey: 'item1',
        firstKey: 'firstMMTItem1',
        midKey: 'midMMTItem1',
        lastKey: 'lastMMTItem1',
        rowspan: 4
    },
    {
        part: '肩',
        content: '外展',
        editKey: 'item2',
        firstKey: 'firstMMTItem2',
        midKey: 'midMMTItem2',
        lastKey: 'lastMMTItem2',
        rowspan: 0
    },
    {
        part: '肩',
        content: '内旋',
        editKey: 'item3',
        firstKey: 'firstMMTItem3',
        midKey: 'midMMTItem3',
        lastKey: 'lastMMTItem3',
        rowspan: 0
    },
    {
        part: '肩',
        content: '外旋',
        editKey: 'item4',
        firstKey: 'firstMMTItem4',
        midKey: 'midMMTItem4',
        lastKey: 'lastMMTItem4',
        rowspan: 0
    },
    {
        part: '肘',
        content: '屈',
        editKey: 'item5',
        firstKey: 'firstMMTItem5',
        midKey: 'midMMTItem5',
        lastKey: 'lastMMTItem5',
        rowspan: 2
    },
    {
        part: '肘',
        content: '伸',
        editKey: 'item6',
        firstKey: 'firstMMTItem6',
        midKey: 'midMMTItem6',
        lastKey: 'lastMMTItem6',
        rowspan: 0
    },
    {
        part: '臂',
        content: '旋前',
        editKey: 'item7',
        firstKey: 'firstMMTItem7',
        midKey: 'midMMTItem7',
        lastKey: 'lastMMTItem7',
        rowspan: 2
    },
    {
        part: '臂',
        content: '旋后',
        editKey: 'item8',
        firstKey: 'firstMMTItem8',
        midKey: 'midMMTItem8',
        lastKey: 'lastMMTItem8',
        rowspan: 0
    },
    {
        part: '腕',
        content: '伸',
        editKey: 'item9',
        firstKey: 'firstMMTItem9',
        midKey: 'midMMTItem9',
        lastKey: 'lastMMTItem9',
        rowspan: 2
    },
    {
        part: '腕',
        content: '屈',
        editKey: 'item10',
        firstKey: 'firstMMTItem10',
        midKey: 'midMMTItem10',
        lastKey: 'lastMMTItem10',
        rowspan: 0
    },
    {
        part: '指',
        content: '伸',
        editKey: 'item11',
        firstKey: 'firstMMTItem11',
        midKey: 'midMMTItem11',
        lastKey: 'lastMMTItem11',
        rowspan: 2
    },
    {
        part: '指',
        content: '屈',
        editKey: 'item12',
        firstKey: 'firstMMTItem12',
        midKey: 'midMMTItem12',
        lastKey: 'lastMMTItem12',
        rowspan: 0
    },
    {
        part: '髋',
        content: '屈',
        editKey: 'item13',
        firstKey: 'firstMMTItem13',
        midKey: 'midMMTItem13',
        lastKey: 'lastMMTItem13',
        rowspan: 4
    },
    {
        part: '髋',
        content: '伸',
        editKey: 'item14',
        firstKey: 'firstMMTItem14',
        midKey: 'midMMTItem14',
        lastKey: 'lastMMTItem14',
        rowspan: 0
    },
    {
        part: '髋',
        content: '内收',
        editKey: 'item15',
        firstKey: 'firstMMTItem15',
        midKey: 'midMMTItem15',
        lastKey: 'lastMMTItem15',
        rowspan: 0
    },
    {
        part: '髋',
        content: '外展',
        editKey: 'item16',
        firstKey: 'firstMMTItem16',
        midKey: 'midMMTItem16',
        lastKey: 'lastMMTItem16',
        rowspan: 0
    },
    {
        part: '膝',
        content: '屈',
        editKey: 'item17',
        firstKey: 'firstMMTItem17',
        midKey: 'midMMTItem17',
        lastKey: 'lastMMTItem17',
        rowspan: 2
    },
    {
        part: '膝',
        content: '伸',
        editKey: 'item18',
        firstKey: 'firstMMTItem18',
        midKey: 'midMMTItem18',
        lastKey: 'lastMMTItem18',
        rowspan: 0
    },
    {
        part: '踝',
        content: '背屈',
        editKey: 'item19',
        firstKey: 'firstMMTItem19',
        midKey: 'midMMTItem19',
        lastKey: 'lastMMTItem19',
        rowspan: 2
    },
    {
        part: '踝',
        content: '跖屈',
        editKey: 'item20',
        firstKey: 'firstMMTItem20',
        midKey: 'midMMTItem20',
        lastKey: 'lastMMTItem20',
        rowspan: 0
    }
]
const assessFormStruct3 = [
    {
        part: '肩',
        content: '屈曲',
        levelValue: '0-170',
        leftEditKey: 'leftItem1',
        leftFirstKey: 'leftFirstROMItem1',
        leftMidKey: 'leftMidROMItem1',
        leftLastKey: 'leftLastROMItem1',
        rightEditKey: 'rightItem1',
        rightFirstKey: 'rightFirstROMItem1',
        rightMidKey: 'rightMidROMItem1',
        rightLastKey: 'rightLastROMItem1',
        rowspan: 2
    },
    {
        part: '肩',
        content: '外展',
        levelValue: '0-170',
        leftEditKey: 'leftItem2',
        leftFirstKey: 'leftFirstROMItem2',
        leftMidKey: 'leftMidROMItem2',
        leftLastKey: 'leftLastROMItem2',
        rightEditKey: 'rightItem2',
        rightFirstKey: 'rightFirstROMItem2',
        rightMidKey: 'rightMidROMItem2',
        rightLastKey: 'rightLastROMItem2',
        rowspan: 0
    },
    {
        part: '肘',
        content: '屈曲',
        levelValue: '0-135/150',
        leftEditKey: 'leftItem3',
        leftFirstKey: 'leftFirstROMItem3',
        leftMidKey: 'leftMidROMItem3',
        leftLastKey: 'leftLastROMItem3',
        rightEditKey: 'rightItem3',
        rightFirstKey: 'rightFirstROMItem3',
        rightMidKey: 'rightMidROMItem3',
        rightLastKey: 'rightLastROMItem3',
        rowspan: 1
    },
    {
        part: '腕',
        content: '掌屈/背伸',
        levelValue: '0-80/0-70',
        leftEditKey: 'leftItem4',
        leftFirstKey: 'leftFirstROMItem4',
        leftMidKey: 'leftMidROMItem4',
        leftLastKey: 'leftLastROMItem4',
        rightEditKey: 'rightItem4',
        rightFirstKey: 'rightFirstROMItem4',
        rightMidKey: 'rightMidROMItem4',
        rightLastKey: 'rightLastROMItem4',
        rowspan: 1
    },
    {
        part: '指',
        content: '掌指屈曲',
        levelValue: '0-90',
        leftEditKey: 'leftItem5',
        leftFirstKey: 'leftFirstROMItem5',
        leftMidKey: 'leftMidROMItem5',
        leftLastKey: 'leftLastROMItem5',
        rightEditKey: 'rightItem5',
        rightFirstKey: 'rightFirstROMItem5',
        rightMidKey: 'rightMidROMItem5',
        rightLastKey: 'rightLastROMItem5',
        rowspan: 2
    },
    {
        part: '指',
        content: '指间屈曲',
        levelValue: '0-110',
        leftEditKey: 'leftItem6',
        leftFirstKey: 'leftFirstROMItem6',
        leftMidKey: 'leftMidROMItem6',
        leftLastKey: 'leftLastROMItem6',
        rightEditKey: 'rightItem6',
        rightFirstKey: 'rightFirstROMItem6',
        rightMidKey: 'rightMidROMItem6',
        rightLastKey: 'rightLastROMItem6',
        rowspan: 0
    },
    {
        part: '髋',
        content: '屈曲',
        levelValue: '0-120',
        leftEditKey: 'leftItem7',
        leftFirstKey: 'leftFirstROMItem7',
        leftMidKey: 'leftMidROMItem7',
        leftLastKey: 'leftLastROMItem7',
        rightEditKey: 'rightItem7',
        rightFirstKey: 'rightFirstROMItem7',
        rightMidKey: 'rightMidROMItem7',
        rightLastKey: 'rightLastROMItem7',
        rowspan: 4
    },
    {
        part: '髋',
        content: '伸展',
        levelValue: '0-30',
        leftEditKey: 'leftItem8',
        leftFirstKey: 'leftFirstROMItem8',
        leftMidKey: 'leftMidROMItem8',
        leftLastKey: 'leftLastROMItem8',
        rightEditKey: 'rightItem8',
        rightFirstKey: 'rightFirstROMItem8',
        rightMidKey: 'rightMidROMItem8',
        rightLastKey: 'rightLastROMItem8',
        rowspan: 0
    },
    {
        part: '髋',
        content: '外展',
        levelValue: '0-40',
        leftEditKey: 'leftItem9',
        leftFirstKey: 'leftFirstROMItem9',
        leftMidKey: 'leftMidROMItem9',
        leftLastKey: 'leftLastROMItem9',
        rightEditKey: 'rightItem9',
        rightFirstKey: 'rightFirstROMItem9',
        rightMidKey: 'rightMidROMItem9',
        rightLastKey: 'rightLastROMItem9',
        rowspan: 0
    },
    {
        part: '髋',
        content: '内收',
        levelValue: '0-35',
        leftEditKey: 'leftItem10',
        leftFirstKey: 'leftFirstROMItem10',
        leftMidKey: 'leftMidROMItem10',
        leftLastKey: 'leftLastROMItem10',
        rightEditKey: 'rightItem10',
        rightFirstKey: 'rightFirstROMItem10',
        rightMidKey: 'rightMidROMItem10',
        rightLastKey: 'rightLastROMItem10',
        rowspan: 0
    },
    {
        part: '膝',
        content: '屈曲',
        levelValue: '0-135',
        leftEditKey: 'leftItem11',
        leftFirstKey: 'leftFirstROMItem11',
        leftMidKey: 'leftMidROMItem11',
        leftLastKey: 'leftLastROMItem11',
        rightEditKey: 'rightItem11',
        rightFirstKey: 'rightFirstROMItem11',
        rightMidKey: 'rightMidROMItem11',
        rightLastKey: 'rightLastROMItem11',
        rowspan: 1
    },
    {
        part: '踝',
        content: '背屈/跖屈',
        levelValue: '0-15/0-50',
        leftEditKey: 'leftItem12',
        leftFirstKey: 'leftFirstROMItem12',
        leftMidKey: 'leftMidROMItem12',
        leftLastKey: 'leftLastROMItem12',
        rightEditKey: 'rightItem12',
        rightFirstKey: 'rightFirstROMItem12',
        rightMidKey: 'rightMidROMItem12',
        rightLastKey: 'rightLastROMItem12',
        rowspan: 1
    }
]
export default {
    name: 'NerveInjury',
    props: {
        formType: {
            type: String,
            default: 'template' // 表单类型：template：模板，edit：评估，view：详情
        },
        patientInfo: {
            type: Object,
            default: () => ({}) // 患者信息
        },
        doctorType: {
            type: String,
            default: 'hospital' // 医生类型：hospital：住院医生，outpatient：门诊医生，therapists：治疗师
        },
        assessInfo: {
            type: Object,
            default: () => ({}) // 评定表信息
        }
    },
    data() {
        return {
            typeList,
            tensionValueList,
            unarmedValueList,
            assessFormStruct1,
            assessFormStruct2,
            assessFormStruct3,
            selfFormData: {},
            rules: {
                estimateType: [
                    { required: true, message: '请选择评定阶段', trigger: 'change' }
                ],
                tensionType: [
                    { required: true, message: '请选择肌张力检测侧', trigger: 'change' }
                ],
                unarmedType: [
                    { required: true, message: '请选择徒手肌力检测侧', trigger: 'change' }
                ],
                ashworth: [
                    { validator: this.validateAshworth, trigger: 'change' }
                ],
                mmt: [
                    { validator: this.validateMMT, trigger: 'change' }
                ],
                rom: [
                    { validator: this.validateROM, trigger: 'change' }
                ]
            },
            firstAssess: true,
            validItems: []
        }
    },
    computed: {
        patientStruct() {
            if (this.formType === 'edit') {
                if (['hospital', 'therapists'].includes(this.doctorType)) {
                    return [{
                        label: '姓名',
                        key: 'patientName'
                    }, {
                        label: '病区',
                        key: 'sickRegion'
                    }, {
                        label: '床号',
                        key: 'bedNum'
                    }, {
                        label: '住院号',
                        key: 'admissionNum'
                    }]
                } else {
                    return [{
                        label: '姓名',
                        key: 'patientName'
                    }, {
                        label: '门诊号',
                        key: 'admissionNum'
                    }]
                }
            } else if (this.formType === 'view') {
                const struct = [{
                    label: '姓名',
                    key: 'patientName'
                }, {
                    label: '性别',
                    key: 'genderName'
                }, {
                    label: '年龄',
                    key: 'age',
                    unit: '岁'
                }, {
                    label: '科室',
                    key: 'departmentName'
                }]
                if (['hospital', 'therapists'].includes(this.doctorType)) {
                    return [
                        ...struct,
                        {
                            label: '床号',
                            key: 'bedNum'
                        }, {
                            label: '住院号',
                            key: 'admissionNum'
                        }
                    ]
                } else {
                    return [
                        ...struct,
                        {
                            label: '门诊号',
                            key: 'admissionNum'
                        }
                    ]
                }
            } else {
                return [{
                    label: '姓名',
                    key: 'patientName'
                }, {
                    label: '门诊号/住院号',
                    key: 'outpatientNo'
                }]
            }
        }
    },
    watch: {
        assessInfo: function (val) {
            this.initFormData()
        }
    },
    created() {
        this.initFormData()
    },
    methods: {
        initTableData(currentAssessInfo, data, tableList, structList, findFunc, isEdit) {
            if (tableList && tableList.length) {
                data === null && (data = {})
                tableList.forEach(item => {
                    const assess = structList.find(assess => findFunc(assess, item))
                    if (!assess) {
                        return
                    }
                    if (isEdit) {
                        data[assess.editKey] = parseFloat(item.partsValue)
                    } else {
                        switch (currentAssessInfo.estimateType) {
                            case 1:
                                data[assess.firstKey] = item.partsValue
                                break
                            case 2:
                                data[assess.midKey] = item.partsValue
                                break
                            case 3:
                                data[assess.lastKey] = item.partsValue
                                break
                        }
                    }
                })
            }
            return data
        },
        initRomData(currentAssessInfo, rom, isEdit) {
            if (currentAssessInfo.jointMobilityList && currentAssessInfo.jointMobilityList.length) {
                rom === null && (rom = {})
                currentAssessInfo.jointMobilityList.forEach(item => {
                    const assess = assessFormStruct3.find(assess => item.partsName.endsWith(assess.part + '：' + assess.content))
                    if (!assess) {
                        return
                    }
                    if (item.partsName.startsWith('左侧：')) {
                        if (isEdit) {
                            rom[assess.leftEditKey] = item.partsValue
                        } else {
                            switch (currentAssessInfo.estimateType) {
                                case 1:
                                    rom[assess.leftFirstKey] = item.partsValue
                                    break
                                case 2:
                                    rom[assess.leftMidKey] = item.partsValue
                                    break
                                case 3:
                                    rom[assess.leftLastKey] = item.partsValue
                                    break
                            }
                        }
                    } else if (item.partsName.startsWith('右侧：')) {
                        if (isEdit) {
                            rom[assess.rightEditKey] = item.partsValue
                        } else {
                            switch (currentAssessInfo.estimateType) {
                                case 1:
                                    rom[assess.rightFirstKey] = item.partsValue
                                    break
                                case 2:
                                    rom[assess.rightMidKey] = item.partsValue
                                    break
                                case 3:
                                    rom[assess.rightLastKey] = item.partsValue
                                    break
                            }
                        }
                    }
                })
            }
            return rom
        },
        initFormData() {
            this.firstAssess = true
            this.selfFormData = Object.assign(initDefaultForm(), this.assessInfo)
            const mainAssessInfo = this.assessInfo
            let ashworth = null
            let mmt = null
            let rom = null
            ashworth = this.initTableData(mainAssessInfo, ashworth, mainAssessInfo.tensionList, assessFormStruct1, (assess, item) => assess.content === item.partsName, true)
            mmt = this.initTableData(mainAssessInfo, mmt, mainAssessInfo.unarmedList, assessFormStruct2, (assess, item) => (assess.part + '：' + assess.content) === item.partsName, true)
            rom = this.initRomData(mainAssessInfo, rom, true)
            if (mainAssessInfo.evaluateDetail && mainAssessInfo.evaluateDetail.length) {
                this.firstAssess = false
                let stageList = [1, 2, 3]
                mainAssessInfo.evaluateDetail.forEach(evaluate => {
                    stageList = stageList.filter(item => item !== evaluate.estimateType)
                    this.selfFormData['evaluateUser' + evaluate.estimateType] = evaluate.evaluateUser
                    this.selfFormData['evaluateTime' + evaluate.estimateType] = moment(evaluate.evaluateTime).format('YYYY-MM-DD')
                    this.selfFormData.diagnosticInfo = evaluate.diagnosticInfo
                    this.selfFormData.estimateType = evaluate.estimateType
                    this.selfFormData.tensionType = evaluate.tensionType
                    this.selfFormData.unarmedType = evaluate.unarmedType
                    ashworth = this.initTableData(evaluate, ashworth, evaluate.tensionList, assessFormStruct1, (assess, item) => assess.content === item.partsName, false)
                    mmt = this.initTableData(evaluate, mmt, evaluate.unarmedList, assessFormStruct2, (assess, item) => (assess.part + '：' + assess.content) === item.partsName, false)
                    rom = this.initRomData(evaluate, rom, false)
                })
                if (stageList.length === 1) {
                    this.selfFormData.estimateType = stageList[0]
                }
            }

            if (ashworth) {
                this.selfFormData.ashworth = ashworth
            }
            if (mmt) {
                this.selfFormData.mmt = mmt
            }
            if (rom) {
                this.selfFormData.rom = rom
            }
        },
        getLabelByValue(list, val) {
            const item = list.find(item => item.value === parseInt(val))
            return item ? item.label : ''
        },
        getFormData() {
            return new Promise((resolve, reject) => {
                this.$refs.assessForm.validate((valid) => {
                    if (valid) {
                        const tensionList = []
                        this.assessFormStruct1.forEach(assess => {
                            tensionList.push({
                                partsName: assess.content,
                                partsValue: this.selfFormData.ashworth[assess.editKey]
                            })
                        })

                        const unarmedList = []
                        this.assessFormStruct2.forEach(assess => {
                            unarmedList.push({
                                partsName: assess.part + '：' + assess.content,
                                partsValue: this.selfFormData.mmt[assess.editKey]
                            })
                        })

                        const jointMobilityList = []
                        this.assessFormStruct3.forEach(assess => {
                            jointMobilityList.push({
                                partsName: '左侧：' + assess.part + '：' + assess.content,
                                partsValue: this.selfFormData.rom[assess.leftEditKey].trim()
                            })
                            jointMobilityList.push({
                                partsName: '右侧：' + assess.part + '：' + assess.content,
                                partsValue: this.selfFormData.rom[assess.rightEditKey].trim()
                            })
                        })
                        resolve({
                            estimateType: this.selfFormData.estimateType,
                            tensionType: this.selfFormData.tensionType,
                            unarmedType: this.selfFormData.unarmedType,
                            diagnosticInfo: this.selfFormData.diagnosticInfo,
                            tensionList,
                            unarmedList,
                            jointMobilityList
                        })
                    } else {
                        reject('存在未通过表单校验的数据')
                        return false
                    }
                })
            })
        },
        changeToEstimateList(estimateId, submitForm) {
            const keyList = []
            Object.keys(submitForm).forEach(key => {
                if(submitForm[key]){
                    keyList.push({
                        key,
                        value: JSON.stringify(submitForm[key])
                    })
                }
            })
            return JSON.stringify({
                estimateId,
                keyList
            })
        },
        async saveLocalFormData(source, patientId) {
            let data = await this.getFormData();
            const estimateId = 8
            const submitForm = {
                evaluateSource: source,
                ...data
            }
            localStorage.setItem(`estimate_${source}_${patientId}_${estimateId}`, this.changeToEstimateList(estimateId, submitForm)); 
        },
        isValidateItem(index, direct){
            let existIndex = this.validItems.findIndex(item => item.index == index && item.direct == direct)
            return existIndex >= 0
        },
        validateAshworth(rule, value, callback) {
            for (let index = 1; index <= 8; index++) {
                if (this.selfFormData.ashworth['item' + index] === undefined) {
                    callback('请选择' + assessFormStruct1[index - 1].content + '的评估等级')
                    return
                }
            }
            callback()
        },
        validateMMT(rule, value, callback) {
            for (let index = 1; index <= 20; index++) {
                if (this.selfFormData.mmt['item' + index] === undefined) {
                    callback('请选择' + assessFormStruct2[index - 1].part + '：' + assessFormStruct2[index - 1].content + '的评估等级')
                    return
                }
            }
            callback()
        },
        validateLevelValue(value, ruleArr) {
            if (!value) {
                return false
            }
            const valueArray = value.split('/')
            return ruleArr.some((item, index) => {
                const range = item.split('-')
                if (!valueArray[index] || !/^\d+(.[0-9]*){0,1}$/.test(valueArray[index])) {
                    return true
                }
                if (range.length === 1) {
                    return valueArray[index] !== range[0]
                } else if (range.length === 2) {
                    return parseFloat(valueArray[index]) < parseFloat(range[0]) || parseFloat(valueArray[index]) > parseFloat(range[1])
                }
                return false
            })
        },
        validateROM(rule, value, callback) {
            this.validItems = []
            let hasError = false
            for (let index = 1; index <= 12; index++) {
                const limit = assessFormStruct3[index - 1].levelValue
                const limitArr = limit.split('/')
                const leftValue = this.selfFormData.rom['leftItem' + index].trim()
                const rightValue = this.selfFormData.rom['rightItem' + index].trim()
                if (!leftValue) {
                    if(!hasError){
                        callback('请填写左侧' + assessFormStruct3[index - 1].part + '：' + assessFormStruct3[index - 1].content + '的活动度')
                        hasError = true
                    }
                } else if (this.validateLevelValue(leftValue, limitArr)) {
                    if(!hasError){
                        callback('填写的左侧' + assessFormStruct3[index - 1].part + '：' + assessFormStruct3[index - 1].content + '的活动度不符合活动度填写范围')
                        hasError = true
                    }
                } else {
                    this.validItems.push({
                        index: index - 1,
                        direct: 'left'
                    })
                }
                if (!rightValue) {
                    if(!hasError){
                        callback('请填写右侧' + assessFormStruct3[index - 1].part + '：' + assessFormStruct3[index - 1].content + '的活动度')
                        hasError = true
                    }
                } else if (this.validateLevelValue(rightValue, limitArr)) {
                    if(!hasError){
                        callback('填写的右侧' + assessFormStruct3[index - 1].part + '：' + assessFormStruct3[index - 1].content + '的活动度不符合活动度填写范围')
                        hasError = true
                    }
                } else {
                    this.validItems.push({
                        index: index - 1,
                        direct: 'right'
                    })
                }
            }
            if(!hasError){
                callback()
            }
        }
    }
}
</script>

<style lang="scss" scoped>
@import url('./style.scss');

.nerve_injury {
    .form_diagnosis {
        display: flex;
        margin-bottom: 10px;
    }

    .form_table {
        margin: 0;
    }

    .assess_term {
        .el-radio {
            margin-bottom: 10px;
        }

        .term_item {
            display: inline-flex;
            padding-top: 1px;

            .term_name,
            .term_time,
            .term_sign {
                width: 240px;
            }
        }
    }

    .score_radio {
        .el-radio {
            margin-right: 10px;
            margin-top: 5px;
            margin-bottom: 5px;
        }
    }
    ::v-deep .el-form-item.is-error .valid_input .el-input__inner {
        border-color: #DCDFE6;
    }
}

</style>
